# -*- coding: UTF-8 -*-



import requests
from bs4 import BeautifulSoup
import re, json, csv
import threadpool
from urllib import parse

headers={
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36',
    'cookie': '__jdu=1606111750160795234393; shshshfpa=7be19f1f-9945-362b-f534-394ce0d824fe-1609822885; shshshfpb=zikFGBo8xQlv%2FS2ORx0K5Hw%3D%3D; unpl=V2_ZzNtbRIEQRQgChFQex8OB2IBFV4SVxRAdlgVVX9NW1E3BxAJclRCFnUUR1ZnGVkUZwAZXkJcQhNFCEdkeBBVAWMDE1VGZxBFLV0CFSNGF1wjU00zQwBBQHcJFF0uSgwDYgcaDhFTQEJ2XBVQL0oMDDdRFAhyZ0AVRQhHZHseXQJnAxJbRlZDEnQOTlB4G1kHbwEibUVncyV1CUVdeBpsBFcCIh8WC0UTcgFGGXseXQJnAxJbRlZDEnQOTlB4G1kHbwEiXHJU; __jdv=76161171|baidu-pinzhuan|t_288551095_baidupinzhuan|cpc|0f3d30c8dba7459bb52f2eb5eba8ac7d_0_ab30d3f517c34362a1fd2ab05e6ea53e|1615695667700; areaId=1; ipLoc-djd=1-2801-0-0; PCSYCityID=CN_0_0_0; __jda=122270672.1606111750160795234393.1606111750.1609822915.1615695668.5; __jdc=122270672; shshshfp=8008acb9b4c03653be5530e1ddfc302f; rkv=1.0; qrsc=3; __jdb=122270672.5.1606111750160795234393|5.1615695668; shshshsID=2186594df62abb2bd9ac7479b9c23d88_5_1615696275986; 3AB9D23F7A4B3C9B=3DDWPUWMLSMRWFZCCL5CLDBDHX3OU5NMD2Z7AGANANPFNIQNWKLUDWAOWOHC7XGYQASJUSEBJDVDBITM3XB6I3OVPY',

}

#https://p.3.cn/prices/mgets?skuids=J_11993134%2C    价格位置


#获取商品信息
def parse_link(link):
    page=requests.get(link,headers=headers)
    soup=BeautifulSoup(page.text,'lxml')
    names=soup.select('div.sku-name')
    for n in names:
        name=n.text.strip()
        print(name)
    zuozhes=soup.select('#p-author')
    for zuozhe in zuozhes:
        print(zuozhe.text.strip())
    chubanshe=soup.select('#parameter2 > li:nth-child(1) > a')
    for chuban in chubanshe:
        print(chuban.text.strip())

#https://item.jd.com/11993134.html       每一页


    #获取价格   和  评论
def price_num(id):
    price_link = 'https://p.3.cn/prices/mgets?skuids=J_{}%2C'.format(id[0])
    price = requests.get(price_link).text

    p = re.compile('"p":"(.*?)"')
    m = re.compile('"m":"(.*?)"')

    price1 = p.findall(price)[0]
    price2 = m.findall(price)[0]
    print('现价'+price1+'元','------','原价'+price2+'元')



    #获取评论

    pinglun_link='https://club.jd.com/comment/productPageComments.action?callback=fetchJSON_comment98&productId={}&score=0&sortType=5&page=0&pageSize=10&isShadowSku=0&fold=1'.format(
        id[0])
    pingluns = requests.get(pinglun_link,headers=headers).text

    pl = re.compile('"content":"(.*?)"')
    pinglun= pl.findall(pingluns)
    for txt in pinglun:


        print(txt)

    print('-' * 50)


#获取每个商品的链接 和 ID

def parse_html(url):
    html=requests.get(url,headers=headers)
    soup=BeautifulSoup(html.text,'lxml')

    titles=soup.select('li.gl-item div.p-name a')
    for title in titles:
        try:
            book=title.select_one('em').text
            link='https:'+title['href']
            parse_link(link)


            num = re.compile('\d+')
            id = num.findall(link)

            price_num(id)

        except:
            pass

        # print(book)
        # print('https:'+link)

#需要搜索的关键子  构建 翻页
KEYWORD=parse.quote('python')    #汉字转码
for i in range(1, 5, 2):
    url = 'https://search.jd.com/Search?keyword={}&wq={}&page={}&s=56&click=0'.format(KEYWORD, KEYWORD, i)
    parse_html(url)


